// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

/*
 * @Author: qiulin_JXGM ql1746365080@gmail.com
 * @Date: 2024-10-16 09:35:57
 * @LastEditors: qiulin_JXGM ql1746365080@gmail.com
 * @LastEditTime: 2024-10-16 14:10:36
 * @FilePath: \SGFEM\Import\BDFImport\BulkData\Element\CONRODParser.cpp
 */

#include "Import/BDFImport/BulkData/Element/CONRODParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "Utility/Logging/SGLogger.h"

#include "DataStructure/Input/Element/CONRODData.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Element/IElementService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (CONROD);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

/*
// file://./../../Doc/MarkDown/BulkData/CONROD/CONROD.md>
*/

// 以下需要修改
enum CONRODkey : std::uint8_t
{
    CONRODKEY_EID = 2,  // Unique element identification number. (0 < Integer < 100,000,000)
    CONRODKEY_G1  = 3,  // Grid point identification numbers of connection points. (Integer > 0;  )
    CONRODKEY_G2  = 4,  //
    CONRODKEY_MID = 5,  // Material identification number. (Integer > 0)
    CONRODKEY_A   = 6,  // Area of the rod. (Real)
    CONRODKEY_J   = 7,  // Torsional constant. (Real)
    CONRODKEY_C   = 8,  // Coefficient for torsional stress determination. (Real)
    CONRODKEY_NSM = 9,  // Nonstructural mass per unit length. (Real)
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseCONROD (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                              std::vector<std::string>&                               dividedEntryInfo)
{
    // 创建临时的CONROD数据对象
    SG::DataStructure::FEM::CONRODData tempCONROD;

    // TODO(bug 001) m_domainId的正确填写
    tempCONROD.m_domainId = 1;  // 暂时将m_domainId设置为1，待修复

    int curIDKey = 1;           // 当前词条的索引，初始值为1

    // 遍历分割后的词条信息
    for (auto curStrKey : dividedEntryInfo)
    {
        // 判断当前词条是否为空白
        bool const isEmpty = is_all_space (curStrKey);
        if (isEmpty)
        {
            curIDKey++;  // 如果为空，跳过当前词条，处理下一个词条
            continue;
        }

        // 将当前字符串转换为科学计数法的浮点数

        // 根据当前词条索引，解析并存储对应的数据
        switch (curIDKey)
        {
        case CONRODKEY_EID:
            // 解析元素识别号EID并存储到tempCONROD的m_id
            tempCONROD.m_id = convertToI64 (curStrKey);
            break;
        case CONRODKEY_G1:
            // 解析网格点G1并存储到tempCONROD的m_g[0]
            tempCONROD.m_g[0] = convertToI64 (curStrKey);
            break;
        case CONRODKEY_G2:
            // 解析网格点G2并存储到tempCONROD的m_g[1]
            tempCONROD.m_g[1] = convertToI64 (curStrKey);
            break;
        case CONRODKEY_MID:
            // 解析材料识别号MID并存储到tempCONROD的m_mId
            tempCONROD.m_mId = convertToI64 (curStrKey);
            break;
        case CONRODKEY_A:
            // 解析截面积A并存储到tempCONROD的m_A
            tempCONROD.m_A = convertToReal (curStrKey);
            break;
        case CONRODKEY_J:
            // 解析扭转常数J并存储到tempCONROD的m_J
            tempCONROD.m_J = convertToReal (curStrKey);
            break;
        case CONRODKEY_C:
            // 解析扭转应力系数C并存储到tempCONROD的m_C
            tempCONROD.m_C = convertToReal (curStrKey);
            break;
        case CONRODKEY_NSM:
            // 解析单位长度的非结构质量NSM并存储到tempCONROD的m_NSM
            tempCONROD.m_NSM = convertToReal (curStrKey);
            break;
        default:
            break;
        }

        curIDKey++;  // 处理完当前词条后，索引加1
    }

    // 将解析后的CONROD数据添加到数据库中
    return dbServiceFactory->get<IElementService> ()->Add (tempCONROD);
}